using System;

namespace AntsMap
{
	/// <summary>
	/// Summary description for SolutionPermuter.
	/// </summary>
	public class SolutionPermuter
	{
		private int dimensions;
		private int elements;
		private int[,] solution;
        private Random r;

		public SolutionPermuter()
		{
			//
			// TODO: Add constructor logic here
			//
            r = new Random();
		}

		public SolutionPermuter( int elem, int dim )
		{
			this.dimensions = dim;
			this.elements = elem;
			this.solution = new int[ elem , dim ];
			for( int i = 0; i<elements;i++)
				for(int j=0;j<dimensions;j++)
					solution[i,j] = -1;

            r = new Random();
		}

        private void Reset()
        {
            for (int i = 0; i < elements; i++)
                for (int j = 0; j < dimensions; j++)
                    solution[i, j] = -1;
        }


		private bool SlotAvailable( int col, int index )
		{
			for( int i = 0; i < this.elements; i++ )
			{
				if( this.solution[i,col] == index )
					return false;
			}
			
			return true;
		}

		public int[,] GenerateSolution( )
		{
			for( int i = 0; i < this.elements; i++ )
			{
				for( int j = 0; j < this.dimensions; j++ )
				{
					if( i == 0 )
						this.solution[i,j] = r.Next( 0 , this.elements);
					else
					{
						int index = r.Next( 0 , this.elements);
						while( SlotAvailable( j , index ) == false)
						{
							index = r.Next( 0 , this.elements);
						}
						this.solution[i,j] = index;
					}
				}
			}
            
            int[,] newsol = new int[this.elements, this.dimensions];
            Array.Copy(solution, newsol, this.elements * this.dimensions);
            this.Reset();
			return newsol;
		}

	}
}
